//package com.example.account;
//
//import android.graphics.Color;
//import android.os.Bundle;
//import android.util.Log;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//
//import androidx.fragment.app.Fragment;
//
//import com.example.account.DataBase.AccountData;
//import com.example.account.DataBase.AccountDataBase;
//import com.example.account.DataBase.AccountDataDao;
//import com.github.mikephil.charting.charts.BarChart;
//import com.github.mikephil.charting.charts.LineChart;
//import com.github.mikephil.charting.data.BarData;
//import com.github.mikephil.charting.data.BarDataSet;
//import com.github.mikephil.charting.data.BarEntry;
//import com.github.mikephil.charting.data.Entry;
//import com.github.mikephil.charting.data.LineData;
//import com.github.mikephil.charting.data.LineDataSet;
//import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
//
//import java.util.ArrayList;
//import java.util.Calendar;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//public class IncomeAnalyseFragment extends Fragment {
//    private LineChart lineChart;
//    private BarChart barChart;
//    private AccountDataDao accountDataDao;
//
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.fragment_income_analyse, container, false);
//
//        lineChart = view.findViewById(R.id.line_chart);
//        barChart = view.findViewById(R.id.bar_chart);
//
//        accountDataDao = AccountDataBase.getDatabase(requireContext()).getDataDao();
//
//        int year = 2024; // 设置你需要的年份
//        int month = 11; // 设置你需要的月份
//
//        List<AccountData> monthlyData = accountDataDao.qaskMoneyByMonth(month, year);
//        List<AccountData> dailyData = accountDataDao.qask_by_ymd(0, month, year); // 0 means all days
//
//        // 调试信息：打印读取的数据
//        Log.d("IncomeAnalyseFragment", "Monthly Data: " + monthlyData.size() + " records");
//        for (AccountData data : monthlyData) {
//            Log.d("IncomeAnalyseFragment", data.toString());
//        }
//
//        Log.d("IncomeAnalyseFragment", "Daily Data: " + dailyData.size() + " records");
//        for (AccountData data : dailyData) {
//            Log.d("IncomeAnalyseFragment", data.toString());
//        }
//
//        Map<Integer, Float> dailyIncomes = new HashMap<>();
//        Map<String, Float> categoryIncomes = new HashMap<>();
//
//        // 初始化每天的收入为0
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(year, month - 1, 1); // 月份从0开始，所以需要减1
//        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//        for (int day = 1; day <= daysInMonth; day++) {
//            dailyIncomes.put(day, 0f);
//        }
//
//        // 计算每一天的收入
//        for (int day = 1; day <= daysInMonth; day++) {
//            List<AccountData> dailyDataForDay = accountDataDao.qask_by_ymd(day, month, year);
//            float dailyIncome = 0f;
//            for (AccountData data : dailyDataForDay) {
//                if (data.getType() == 1) { // 收入
//                    dailyIncome += data.getMoney();
//                }
//            }
//            dailyIncomes.put(day, dailyIncome);
//        }
//
//        // 调试信息：打印每一天的收入
//        Log.d("IncomeAnalyseFragment", "Daily Incomes: " + dailyIncomes.size() + " records");
//        for (Map.Entry<Integer, Float> entry : dailyIncomes.entrySet()) {
//            Log.d("IncomeAnalyseFragment", "Day: " + entry.getKey() + ", Income: " + entry.getValue());
//        }
//
//        // 计算每个类别的收入
//        for (AccountData data : monthlyData) {
//            String category = data.getName(); // 假设类别名称存储在name字段中
//            if (data.getType() == 1) { // 收入
//                float currentIncome = categoryIncomes.get(category) != null ? categoryIncomes.get(category) : 0f;
//                categoryIncomes.put(category, currentIncome + data.getMoney());
//            }
//        }
//
//        // 调试信息：打印每个类别的收入
//        Log.d("IncomeAnalyseFragment", "Category Incomes: " + categoryIncomes.size() + " records");
//        for (Map.Entry<String, Float> entry : categoryIncomes.entrySet()) {
//            Log.d("IncomeAnalyseFragment", "Category: " + entry.getKey() + ", Income: " + entry.getValue());
//        }
//
//        setupLineChart(dailyIncomes);
//        setupBarChart(categoryIncomes);
//
//        return view;
//    }
//
//    private void setupLineChart(Map<Integer, Float> dailyIncomes) {
//        List<Entry> incomeEntries = new ArrayList<>();
//        List<String> xAxisLabels = new ArrayList<>();
//
//        for (Map.Entry<Integer, Float> entry : dailyIncomes.entrySet()) {
//            incomeEntries.add(new Entry(entry.getKey(), entry.getValue()));
//            xAxisLabels.add(String.valueOf(entry.getKey()));
//        }
//
//        int color = Color.parseColor("#DFF9DE"); // 解析颜色代码
//        LineDataSet incomeDataSet = new LineDataSet(incomeEntries, "Daily Incomes");
//        incomeDataSet.setColor(Color.GREEN);
//        incomeDataSet.setValueTextColor(Color.BLACK); // 设置数值文字颜色
//        incomeDataSet.setDrawCircles(true); // 显示圆点
//        incomeDataSet.setCircleColor(color); // 设置圆点颜色
//        incomeDataSet.setCircleRadius(3f); // 设置圆点半径
//        incomeDataSet.setLineWidth(1f); // 设置线宽度
//        incomeDataSet.setDrawFilled(true); // 开启填充
//        incomeDataSet.setFillColor(color); // 设置填充颜色
//        incomeDataSet.setFillAlpha(200); // 设置填充透明度，范围为0-255
//        incomeDataSet.setDrawValues(true);
//
//        LineData lineData = new LineData(incomeDataSet);
//        lineChart.setData(lineData);
//        lineChart.getDescription().setEnabled(false); // 不显示图表描述
//        lineChart.getAxisLeft().setEnabled(true); // 启用左侧Y轴
//        lineChart.getAxisRight().setEnabled(false); // 禁用右侧Y轴
//        lineChart.getAxisLeft().setDrawGridLines(false); // 禁用右侧Y轴网格线
//        lineChart.getXAxis().setDrawGridLines(false); // 禁用X轴网格线
//        lineChart.getAxisLeft().setAxisMinimum(0f); // 设置Y轴最小值
//        lineChart.getXAxis().setPosition(com.github.mikephil.charting.components.XAxis.XAxisPosition.BOTTOM); // 设置X轴位置
//        lineChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisLabels));
//        lineChart.getAxisLeft().setAxisLineColor(Color.BLACK); // 设置Y轴颜色
//        lineChart.getXAxis().setAxisLineColor(Color.BLACK); // 设置X轴颜色
//        lineChart.invalidate(); // refresh
//    }
//
//
//    private void setupBarChart(Map<String, Float> categoryIncomes) {
//        List<BarEntry> incomeBarEntries = new ArrayList<>();
//        List<String> xAxisLabels = new ArrayList<>();
//
//        int index = 0;
//        for (Map.Entry<String, Float> entry : categoryIncomes.entrySet()) {
//            incomeBarEntries.add(new BarEntry(index, entry.getValue()));
//            xAxisLabels.add(entry.getKey());
//            index++;
//        }
//
//        BarDataSet incomeBarDataSet = new BarDataSet(incomeBarEntries, "Category Incomes");
//        incomeBarDataSet.setColor(Color.GREEN);
//        incomeBarDataSet.setValueTextColor(Color.BLACK);
//
//        BarData barData = new BarData(incomeBarDataSet);
//        barChart.setData(barData);
//        barChart.getDescription().setEnabled(false); // 不显示图表描述
//        barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisLabels));
//        barChart.getXAxis().setPosition(com.github.mikephil.charting.components.XAxis.XAxisPosition.BOTTOM);
//        barChart.getXAxis().setGranularity(1f);
//        barChart.getXAxis().setLabelCount(xAxisLabels.size());
//        barChart.getXAxis().setDrawGridLines(false); // 禁用X轴网格线
//        barChart.getAxisLeft().setEnabled(true); // 启用左侧Y轴
//        barChart.getAxisRight().setEnabled(false); // 禁用右侧Y轴
//        barChart.getAxisLeft().setDrawGridLines(false); // 禁用右侧Y轴网格线
//        barChart.invalidate(); // refresh
//    }
//}
//










package com.example.account;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.example.account.DataBase.AccountData;
import com.example.account.DataBase.AccountDataBase;
import com.example.account.DataBase.AccountDataDao;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//public class IncomeAnalyseFragment extends Fragment {
//    private LineChart lineChart;
//    private BarChart barChart;
//    private AccountDataDao accountDataDao;
//
//    public void updateData(int year, int month) {
//        List<AccountData> monthlyData = accountDataDao.qaskMoneyByMonth(month, year);
//
//        // 调试信息：打印读取的数据
//        Log.d("IncomeAnalyseFragment", "Monthly Data for " + year + "-" + month + ": " + monthlyData.size() + " records");
//        for (AccountData data : monthlyData) {
//            Log.d("IncomeAnalyseFragment", data.toString());
//        }
//
//        Map<Integer, Float> dailyIncomes = new HashMap<>();
//        Map<String, Float> categoryIncomes = new HashMap<>();
//
//        // 初始化每天的收入为0
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(year, month - 1, 1); // 月份从0开始，所以需要减1
//        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//        for (int day = 1; day <= daysInMonth; day++) {
//            dailyIncomes.put(day, 0f);
//        }
//
//        // 计算每一天的收入
//        for (int day = 1; day <= daysInMonth; day++) {
//            List<AccountData> dailyDataForDay = accountDataDao.qask_by_ymd(day, month, year);
//            float dailyIncome = 0f;
//            for (AccountData data : dailyDataForDay) {
//                if (data.getType() == 1) { // 收入
//                    dailyIncome += data.getMoney();
//                }
//            }
//            dailyIncomes.put(day, dailyIncome);
//        }
//
//        // 计算每个类别的收入
//        for (AccountData data : monthlyData) {
//            String category = data.getName(); // 假设类别名称存储在name字段中
//            if (data.getType() == 1) { // 收入
//                float currentIncome = categoryIncomes.get(category) != null ? categoryIncomes.get(category) : 0f;
//                categoryIncomes.put(category, currentIncome + data.getMoney());
//            }
//        }
//
//        setupLineChart(dailyIncomes);
//        setupBarChart(categoryIncomes);
//    }
//
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.fragment_income_analyse, container, false);
//
//        lineChart = view.findViewById(R.id.line_chart);
//        barChart = view.findViewById(R.id.bar_chart);
//
//        accountDataDao = AccountDataBase.getDatabase(requireContext()).getDataDao();
//
//        // 获取当前系统时间
//        Calendar calendar = Calendar.getInstance();
//        int year = calendar.get(Calendar.YEAR);
//        int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始，所以需要加1
//
//        // 默认加载当前月份的数据
//        updateData(year, month);
//
//        return view;
//    }
//
//    private void setupLineChart(Map<Integer, Float> dailyIncomes) {
//        List<Entry> incomeEntries = new ArrayList<>();
//        List<String> xAxisLabels = new ArrayList<>();
//
//        for (Map.Entry<Integer, Float> entry : dailyIncomes.entrySet()) {
//            incomeEntries.add(new Entry(entry.getKey(), entry.getValue()));
//            xAxisLabels.add(String.valueOf(entry.getKey()));
//        }
//
//        int color = Color.parseColor("#75D173"); // 解析颜色代码
//        LineDataSet incomeDataSet = new LineDataSet(incomeEntries, "Daily Incomes");
//        incomeDataSet.setColor(color);
//        incomeDataSet.setValueTextColor(Color.BLACK);
//        incomeDataSet.setDrawValues(true);  // 显示数值
//        incomeDataSet.setLineWidth(1f); // 设置线宽度
//        incomeDataSet.setDrawCircles(true); // 显示圆点
//        incomeDataSet.setCircleColor(color); // 设置圆点颜色
//        incomeDataSet.setCircleRadius(3f); // 设置圆点半径
//        incomeDataSet.setDrawFilled(true); // 开启填充
//        incomeDataSet.setFillColor(color); // 设置填充颜色
//        incomeDataSet.setFillAlpha(50); // 设置填充透明度，范围为0-255
//
//        LineData lineData = new LineData(incomeDataSet);
//        lineChart.setData(lineData);
//        lineChart.getDescription().setEnabled(false); // 不显示图表描述
//        lineChart.getAxisLeft().setEnabled(true); // 启用左侧Y轴
//        lineChart.getAxisRight().setEnabled(false); // 禁用右侧Y轴
//        lineChart.getAxisLeft().setDrawGridLines(false); // 禁用右侧Y轴网格线
//        lineChart.getXAxis().setDrawGridLines(false); // 禁用X轴网格线
//        lineChart.getAxisLeft().setAxisMinimum(0f); // 设置Y轴最小值
//        lineChart.getXAxis().setPosition(com.github.mikephil.charting.components.XAxis.XAxisPosition.BOTTOM); // 设置X轴位置
//        lineChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisLabels));
//        lineChart.getAxisLeft().setAxisLineColor(Color.BLACK); // 设置Y轴颜色
//        lineChart.getXAxis().setAxisLineColor(Color.BLACK); // 设置X轴颜色
//
//        lineChart.invalidate(); // refresh
//    }
//
//    private void setupBarChart(Map<String, Float> categoryIncomes) {
//        List<BarEntry> incomeBarEntries = new ArrayList<>();
//        List<String> xAxisLabels = new ArrayList<>();
//
//        int index = 0;
//        for (Map.Entry<String, Float> entry : categoryIncomes.entrySet()) {
//            incomeBarEntries.add(new BarEntry(index, entry.getValue()));
//            xAxisLabels.add(entry.getKey());
//            index++;
//        }
//
//        int color = Color.parseColor("#75D173"); // 解析颜色代码
//        BarDataSet incomeBarDataSet = new BarDataSet(incomeBarEntries, "Category Incomes");
//        incomeBarDataSet.setColor(color);
//        incomeBarDataSet.setValueTextColor(Color.BLACK);
//
//        BarData barData = new BarData(incomeBarDataSet);
//        barChart.setData(barData);
//        barChart.getDescription().setEnabled(false); // 不显示图表描述
//        barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisLabels));
//        barChart.getXAxis().setPosition(com.github.mikephil.charting.components.XAxis.XAxisPosition.BOTTOM);
//        barChart.getXAxis().setGranularity(1f);
//        barChart.getXAxis().setLabelCount(xAxisLabels.size());
//        barChart.getXAxis().setDrawGridLines(false); // 禁用X轴网格线
//        barChart.getAxisLeft().setEnabled(true); // 启用左侧Y轴
//        barChart.getAxisRight().setEnabled(false); // 禁用右侧Y轴
//        barChart.getAxisLeft().setDrawGridLines(false); // 禁用右侧Y轴网格线
//        barChart.getAxisLeft().setAxisMinimum(0f); // 设置Y轴最小值
//        barChart.invalidate(); // refresh
//    }
//}



public class IncomeAnalyseFragment extends Fragment {
    private LineChart lineChart;
    private BarChart barChart;
    private AccountDataDao accountDataDao;

    public void updateData(int year, int month) {
        List<AccountData> monthlyData = accountDataDao.qaskMoneyByMonth(month, year);

        // 调试信息：打印读取的数据
        Log.d("IncomeAnalyseFragment", "Monthly Data for " + year + "-" + month + ": " + monthlyData.size() + " records");
        for (AccountData data : monthlyData) {
            Log.d("IncomeAnalyseFragment", data.toString());
        }

        Map<Integer, Float> dailyIncomes = new HashMap<>();
        Map<String, Float> categoryIncomes = new HashMap<>();

        // 初始化每天的收入为0
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1); // 月份从0开始，所以需要减1
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int day = 1; day <= daysInMonth; day++) {
            dailyIncomes.put(day, 0f);
        }

        // 计算每一天的收入
        for (int day = 1; day <= daysInMonth; day++) {
            List<AccountData> dailyDataForDay = accountDataDao.qask_by_ymd(day, month, year);
            float dailyIncome = 0f;
            for (AccountData data : dailyDataForDay) {
                if (data.getType() == 1) { // 收入
                    dailyIncome += data.getMoney();
                }
            }
            dailyIncomes.put(day, dailyIncome);
        }

        // 计算每个类别的收入
        for (AccountData data : monthlyData) {
            String category = data.getName(); // 假设类别名称存储在name字段中
            if (data.getType() == 1) { // 收入
                float currentIncome = categoryIncomes.get(category) != null ? categoryIncomes.get(category) : 0f;
                categoryIncomes.put(category, currentIncome + data.getMoney());
            }
        }

        setupLineChart(dailyIncomes);
        setupBarChart(categoryIncomes);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_income_analyse, container, false);

        lineChart = view.findViewById(R.id.line_chart);
        barChart = view.findViewById(R.id.bar_chart);

        accountDataDao = AccountDataBase.getDatabase(requireContext()).getDataDao();

        // 获取当前系统时间
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始，所以需要加1

        // 默认加载当前月份的数据
        updateData(year, month);

        // 设置 MarkerView
        CustomMarkerView markerView = new CustomMarkerView(getContext(), R.layout.custom_marker_view);
        lineChart.setMarker(markerView);

        return view;
    }

    private void setupLineChart(Map<Integer, Float> dailyIncomes) {
        List<Entry> incomeEntries = new ArrayList<>();
        List<String> xAxisLabels = new ArrayList<>();

        for (Map.Entry<Integer, Float> entry : dailyIncomes.entrySet()) {
            incomeEntries.add(new Entry(entry.getKey(), entry.getValue()));
            xAxisLabels.add(String.valueOf(entry.getKey()));
        }

        int color = Color.parseColor("#75D173"); // 解析颜色代码
        LineDataSet incomeDataSet = new LineDataSet(incomeEntries, "Daily Incomes");
        incomeDataSet.setColor(color);
        incomeDataSet.setValueTextColor(Color.BLACK);
        incomeDataSet.setDrawValues(true);  // 显示数值
        incomeDataSet.setLineWidth(1f); // 设置线宽度
        incomeDataSet.setDrawCircles(true); // 显示圆点
        incomeDataSet.setCircleColor(color); // 设置圆点颜色
        incomeDataSet.setCircleRadius(3f); // 设置圆点半径
        incomeDataSet.setDrawFilled(true); // 开启填充
        incomeDataSet.setFillColor(color); // 设置填充颜色
        incomeDataSet.setFillAlpha(50); // 设置填充透明度，范围为0-255

        LineData lineData = new LineData(incomeDataSet);
        lineChart.setData(lineData);
        lineChart.getDescription().setEnabled(false); // 不显示图表描述
        lineChart.getAxisLeft().setEnabled(true); // 启用左侧Y轴
        lineChart.getAxisRight().setEnabled(false); // 禁用右侧Y轴
        lineChart.getAxisLeft().setDrawGridLines(false); // 禁用右侧Y轴网格线
        lineChart.getXAxis().setDrawGridLines(false); // 禁用X轴网格线
        lineChart.getAxisLeft().setAxisMinimum(0f); // 设置Y轴最小值
        lineChart.getXAxis().setPosition(com.github.mikephil.charting.components.XAxis.XAxisPosition.BOTTOM); // 设置X轴位置
        lineChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisLabels));
        lineChart.getAxisLeft().setAxisLineColor(Color.BLACK); // 设置Y轴颜色
        lineChart.getXAxis().setAxisLineColor(Color.BLACK); // 设置X轴颜色

        lineChart.invalidate(); // refresh
    }

    private void setupBarChart(Map<String, Float> categoryIncomes) {
        List<BarEntry> incomeBarEntries = new ArrayList<>();
        List<String> xAxisLabels = new ArrayList<>();

        int index = 0;
        for (Map.Entry<String, Float> entry : categoryIncomes.entrySet()) {
            incomeBarEntries.add(new BarEntry(index, entry.getValue()));
            xAxisLabels.add(entry.getKey());
            index++;
        }

        int color = Color.parseColor("#75D173"); // 解析颜色代码
        BarDataSet incomeBarDataSet = new BarDataSet(incomeBarEntries, "Category Incomes");
        incomeBarDataSet.setColor(color);
        incomeBarDataSet.setValueTextColor(Color.BLACK);

        BarData barData = new BarData(incomeBarDataSet);
        barChart.setData(barData);
        barChart.getDescription().setEnabled(false); // 不显示图表描述
        barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisLabels));
        barChart.getXAxis().setPosition(com.github.mikephil.charting.components.XAxis.XAxisPosition.BOTTOM);
        barChart.getXAxis().setGranularity(1f);
        barChart.getXAxis().setLabelCount(xAxisLabels.size());
        barChart.getXAxis().setDrawGridLines(false); // 禁用X轴网格线
        barChart.getAxisLeft().setEnabled(true); // 启用左侧Y轴
        barChart.getAxisRight().setEnabled(false); // 禁用右侧Y轴
        barChart.getAxisLeft().setDrawGridLines(false); // 禁用右侧Y轴网格线
        barChart.getAxisLeft().setAxisMinimum(0f); // 设置Y轴最小值
        barChart.invalidate(); // refresh
    }
}